var gulp=require("gulp");
var gulp_min_css=require("gulp-minify-css");
var gulp_sass=require("gulp-sass");
var gulp_plumber=require("gulp-plumber");
var gulp_tinypng=require("gulp-tinypng");
var gulp_uglify=require("gulp-uglify");
var gulpCopy = require('gulp-copy');
var gulp_autoprefixer=require('gulp-autoprefixer');



gulp.task("copy",function(){
    gulp.src("src/fonts/**/*").pipe(gulpCopy("dist/fonts"))
    gulp.src("src/*.html").pipe(gulpCopy("dist",{prefix:"1"}))
})

gulp.task("mincss",function(){
	//这个任务具体要做的事情就在这里写了
	gulp.src('./src/css/**/*.css')
	.pipe(gulp_plumber())
	.pipe(gulp_min_css())
	.pipe(gulp_autoprefixer({
			browsers: ['last 2 versions', 'Android >= 4.0','iOS 7','last 3 Safari versions'],
			cascade: false
		}))
	.pipe(gulp.dest('dist/css'));
})

gulp.task("sass",function(){
	//这个任务具体要做的事情就在这里写了
	gulp.src('src/scss/**/*.scss')
	.pipe(gulp_plumber())
	.pipe(gulp_sass())
	.pipe(gulp.dest('src/css'));
})

gulp.task("watchscss",function(){
	gulp.watch(
		'src/scss/**/*.scss',["sass"]
	)
})
gulp.task("minimg",function(){
	gulp.src('src/img/**/*.*')
	.pipe(gulp_tinypng("dQ5okTCk1kmCe5JwxmYpZaJALFkhX2sX"))
	.pipe(gulp.dest('dist/img'));
})
gulp.task("minjs",function(){
	gulp.src('src/js/**/*.js')
	.pipe(gulp_uglify())
	.pipe(gulp.dest('dist/js'));
})

//定义浏览器刷新任务
var browser_sync=require("browser-sync").create();
gulp.task('browser-sync', function() {
    browser_sync.init({
    	port:Math.floor(Math.random()*9999)+1000,
        server: {
            baseDir: "./src"
        }
    });
    gulp.watch("src/scss/**/*.scss",["sass"]).on("change",browser_sync.reload);
    gulp.watch("src/html/*.html").on("change",browser_sync.reload);
}); 

//定义一键开发模式
gulp.task("dev",["browser-sync"]);

//定义一建生成模式
gulp.task("pro",["mincss","minjs","minimg","copy"]);

